package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.behavior.vos.ArticleBehaviorVO;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {


    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    RedisTemplate<String,String>  redisTemplate;


    @Transactional
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {

       // 1. 判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();

        ArticleBehaviorVO articleBehaviorVO = new ArticleBehaviorVO();
        if(user==null){
            // 2. 未登录 直接将 4种行为 均返回false
            articleBehaviorVO.setIsfollow(false);
            articleBehaviorVO.setIscollection(false);
            articleBehaviorVO.setIslike(false);
            articleBehaviorVO.setIsunlike(false);
            return ResponseResult.okResult(articleBehaviorVO);
        }
       // 3. 如果登录
       // 4. 查询行为实体
        ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserEquipmentId(user.getId(), dto.getEquipmentId());
        // 5. 根据行为实体 文章ID查询 是否点赞
        Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("entryId").is(behaviorEntry.getId()));
        ApLikesBehavior likesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
       articleBehaviorVO.setIslike(likesBehavior!=null);
        // 6. 根据行为实体 文章ID查询 是否不喜欢
        ApUnlikesBehavior behavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        articleBehaviorVO.setIsunlike(behavior!=null);
       // 7. 根据行为实体 文章ID查询 是否收藏
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
        articleBehaviorVO.setIscollection(apCollection!=null);
       // 8. 更具登录用户id 去redis中查询是否关注该作者
        Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + user.getId(), String.valueOf(dto.getAuthorId()));
        articleBehaviorVO.setIsfollow(score!=null);
        // 9. 封装结果 返回
        return ResponseResult.okResult(articleBehaviorVO);
    }
}
